//
//  MainTabBarController.m
//  MtimeMovie
//
//  Created by lllyyy on 15/7/17.
//  Copyright (c) 2015年 lllyyy. All rights reserved.
//

#import "MainTabBarController.h"
#import "TopViewController.h"
#import "NewsViewController.h"
#import "MoreViewController.h"
#import "CinemaViewController.h"
#import "HomeViewController.h"
#import "Button.h"
#import "BaseViewController.h"
#import "BaseNavController.h"
@interface MainTabBarController ()
{
    UIImageView *selectImage;
}

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [self creativeViewController];
    [self setTabbar];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)creativeViewController{
    TopViewController *vc3 = [[TopViewController alloc]init];
    NewsViewController *vc2 = [[NewsViewController alloc]init];
    MoreViewController *vc5 = [[MoreViewController alloc]init];
    CinemaViewController *vc4 = [[CinemaViewController alloc]init];
    HomeViewController *vc1 = [[HomeViewController alloc]init];
    
    NSArray *vcArray = @[vc1,vc2,vc3,vc4,vc5];
    NSMutableArray *navArray = [[NSMutableArray alloc]init];
    for (int i=0;i<5;i++){
        BaseNavController *nav = [[BaseNavController alloc]initWithRootViewController:vcArray[i]];
        [navArray addObject:nav];
    }
    self.viewControllers = navArray;
}

- (void)setTabbar{
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    for(UIView *subview in self.tabBar.subviews){
        Class a = NSClassFromString(@"UITabBarButton");
        if([subview isKindOfClass:a]){
            [subview removeFromSuperview];
        }
        
    }
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    
    selectImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    selectImage.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    [self.tabBar addSubview:selectImage];
    NSArray *imageName = @[@"movie_home",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"];
    NSArray *title = @[@"首页",@"新闻",@"TOP",@"影院",@"更多"];
    for(int i = 0 ;i <5; i++){
        CGRect frame = CGRectMake(i*width, 0, width, height);
        Button *button = [[Button alloc]initWithFrame:frame withImageName:imageName[i] withTitle:title[i]];
        button.tag = i;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }
}
- (void)buttonPressed:(UIButton *)btn{
    NSInteger index = btn.tag;
    self.selectedIndex = index;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    selectImage.center = btn.center;
    [UIView commitAnimations];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
